package com.wwwarehouse.taskcenter.fragment.job_point.step5;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStep5LookItemMesResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/25 18:53
 * @描述 ${TODO}
 */

public class ChooseJobUnitpagerFragment5 extends CommonBasePagerFragment implements View.OnClickListener {

    private Button mBtSave;
    public static final int REQUEST_1 = 1003;
    private ArrayList<AddJobPointStep5LookItemMesResponseBean.ListBean> listBean;
    private ArrayList<AddJobPointStep5LookItemMesResponseBean.ListBean> selectListBean = new ArrayList<>();
    private AddJobPointStep5LookItemMesResponseBean selectBean = new AddJobPointStep5LookItemMesResponseBean();

    private Map<String,AddJobPointStep5LookItemMesResponseBean.ListBean> listBeanMap = new HashMap<>();
    private String jobPointUkid;
    private String warehouseUkid;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseJobUnitpagerFragment5) {
            mActivity.setTitle("" + getString(R.string.new_task_step5_title_txt));
        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        jobPointUkid = getArguments().getString("jobPointUkid");
        warehouseUkid = getArguments().getString("warehouseUkid");

        if(StringUtils.isNullString(warehouseUkid)){
            warehouseUkid = BaseApplication.sp.getValue("warehouseUkid");
        }

        showConfirmButton();
        setConfirmBtnText("" + getString(R.string.new_task_save));
        mBtSave = (Button) getConfirmBtn();
        mBtSave.setOnClickListener(this);
        requestHttp();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDownPull() {
    }



    private void initPager() {
        Bundle budle = new Bundle();

        AddJobPointStep5LookItemMesResponseBean.ListBean listBeanPeople  = new AddJobPointStep5LookItemMesResponseBean.ListBean();
        listBeanPeople.setUnitUkid("");
        listBeanPeople.setUnitName("");
        listBeanPeople.setCategoryName("人");
        listBeanPeople.setMetaCategoryUkid("999");

        ArrayList<AddJobPointStep5LookItemMesResponseBean.ListBean> lists = new ArrayList<>();
        if(listBean!=null){
            lists = listBean;
            lists.add(0,listBeanPeople);
        }


        try {
            setData(lists.size(), 10, "com/wwwarehouse/taskcenter/fragment/job_point/step5/ChooseJobUnitpagerDetailFragment5", budle, lists);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.showErrLog("error:"+e.getMessage());
        }
    }


    @Override
    public void onRefreshListener() {
        requestHttp();
    }

    @Override
    public void onLoadSuccess(String s, int i) {
        if (REQUEST_1 == i) {
            listBean = (ArrayList<AddJobPointStep5LookItemMesResponseBean.ListBean>) JSON.parseArray(s,AddJobPointStep5LookItemMesResponseBean.ListBean.class);
            initPager();
        }
    }

    @Override
    public void onLoadError(String s) {

    }

    /**
     *  查询类目
     */
    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        /**
         * warehouseUkid	概念仓库id	number
         */
        map.put("warehouseUkid",warehouseUkid);
        loadData(TaskCenterConstant.URL_GET_CATEGORY_INFO, map, REQUEST_1);
    }

    private void selectAll(){
        //router/api?method=ImAbstractObject.selectImAbstractObjects&version=1.0.0&t=134333333&w_appid=wh_iphone_1.0
        Map<String, Object> map = new HashMap<>();
        /**
         * warehouseUkid	概念仓库id	number
         */
        map.put("warehouseUkid",warehouseUkid);
    }


    @Override
    public void onClick(View v) {
        if (v == mBtSave) {

            listBeanMap.keySet();
            selectListBean.clear();
            for(String str : listBeanMap.keySet()){
                selectListBean.add(listBeanMap.get(str));
            }
            selectBean.setList(selectListBean);
//            DialogTools.getInstance().showCustomDialogPrompt(mActivity, "提示", "确认保存所选关联生产物吗？", false, new DialogTools.ConfirmListener() {
//                @Override
//                public void setConfirmListener(Dialog dialog, View view, String s) {
//
//            dialog.dismiss();
//
//                }
//            }, "保存", "取消");

            //确定
            Bundle bundle = new Bundle();
            bundle.putString("jobPointUkid",jobPointUkid);
            bundle.putParcelable("selectBean" , selectBean);
            ChooseJobUnitSetnumberpagerFragment5  fragment5 = new ChooseJobUnitSetnumberpagerFragment5();
            fragment5.setArguments(bundle);
            pushFragment(fragment5);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(AddJobPointStep5LookItemMesResponseBean.ListBean bean) {

        if(listBeanMap.containsKey(bean.getMetaCategoryUkid())){ // 含有就删除 不含有添加
            Iterator<Map.Entry<String, AddJobPointStep5LookItemMesResponseBean.ListBean>> iterator = listBeanMap.entrySet().iterator();
            while (iterator.hasNext()){
                if((""+bean.getMetaCategoryUkid()).equals(iterator.next().getValue().getMetaCategoryUkid())){
                    iterator.remove();
                }
            }
        }else{
            listBeanMap.put(""+ bean.getMetaCategoryUkid(),bean);
        }
    }

}
